home *** CD-ROM | disk | FTP | other *** search
/ Hot Super Models / Hot Super Models.iso / dos / tif / jrdgif.c < prev    next >
C/C++ Source or Header  |  1992-07-04  |  21KB  |  634 lines

  1. /*
  2.  * jrdgif.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to read input images in GIF format.
  9.  *
  10.  * These routines may need modification for non-Unix environments or
  11.  * specialized applications.  As they stand, they assume input from
  12.  * an ordinary stdio stream.  They further assume that reading begins
  13.  * at the start of the file; input_init may need work if the
  14.  * user interface has already read some data (e.g., to determine that
  15.  * the file is indeed GIF format).
  16.  *
  17.  * These routines are invoked via the methods get_input_row
  18.  * and input_init/term.
  19.  */
  20.  
  21. /*
  22.  * This code is loosely based on giftoppm from the PBMPLUS distribution
  23.  * of Feb. 1991.  That file contains the following copyright notice:
  24.  * +-------------------------------------------------------------------+
  25.  * | Copyright 1990, David Koblas.                                     |
  26.  * |   Permission to use, copy, modify, and distribute this software   |
  27.  * |   and its documentation for any purpose and without fee is hereby |
  28.  * |   granted, provided that the above copyright notice appear in all |
  29.  * |   copies and that both that copyright notice and this permission  |
  30.  * |   notice appear in supporting documentation.  This software is    |
  31.  * |   provided "as is" without express or implied warranty.           |
  32.  * +-------------------------------------------------------------------+
  33.  *
  34.  * We are also required to state that
  35.  *    "The Graphics Interchange Format(c) is the Copyright property of
  36.  *    CompuServe Incorporated. GIF(sm) is a Service Mark property of
  37.  *    CompuServe Incorporated."
  38.  */
  39.  
  40. #include "jinclude.h"
  41. #include "grdriver.h"
  42. #include "grx.h"
  43.  
  44. #ifdef GIF_SUPPORTED
  45.  
  46.  
  47. #define    MAXCOLORMAPSIZE    256    /* max # of colors in a GIF colormap */
  48. #define NUMCOLORS    3    /* # of colors */
  49. #define CM_RED        0    /* color component numbers */
  50. #define CM_GREEN    1
  51. #define CM_BLUE        2
  52.  
  53. JSAMPARRAY gifcolormap; /* the colormap to use */
  54. /* colormap[i][j] = value of i'th color component for pixel value j */
  55.  
  56. #define    MAX_LZW_BITS    12    /* maximum LZW code size */
  57. #define LZW_TABLE_SIZE    (1<<MAX_LZW_BITS) /* # of possible LZW symbols */
  58.  
  59. /* Macros for extracting header data --- note we assume chars may be signed */
  60.  
  61. #define LM_to_uint(a,b)        ((((b)&0xFF) << 8) | ((a)&0xFF))
  62.  
  63. #define BitSet(byte, bit)    ((byte) & (bit))
  64. #define INTERLACE    0x40    /* mask for bit signifying interlaced image */
  65. #define COLORMAPFLAG    0x80    /* mask for bit signifying colormap presence */
  66.  
  67. #define    ReadOK(file,buffer,len)    (JFREAD(file,buffer,len) == ((size_t) (len)))
  68.  
  69. /* Static vars for GetCode and LZWReadByte */
  70.  
  71. static char code_buf[256+4];    /* current input data block */
  72. static int last_byte;        /* # of bytes in code_buf */
  73. static int last_bit;        /* # of bits in code_buf */
  74. static int cur_bit;        /* next bit index to read */
  75. static boolean out_of_blocks;    /* TRUE if hit terminator data block */
  76.  
  77. static int input_code_size;    /* codesize given in GIF file */
  78. static int clear_code,end_code; /* values for Clear and End codes */
  79.  
  80. static int code_size;        /* current actual code size */
  81. static int limit_code;        /* 2^code_size */
  82. static int max_code;        /* first unused code value */
  83. static boolean first_time;    /* flags first call to LZWReadByte */
  84.  
  85. /* LZW decompression tables:
  86.  *   symbol_head[K] = prefix symbol of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  87.  *   symbol_tail[K] = suffix byte   of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  88.  * Note that entries 0..end_code of the above tables are not used,
  89.  * since those symbols represent raw bytes or special codes.
  90.  *
  91.  * The stack represents the not-yet-used expansion of the last LZW symbol.
  92.  * In the worst case, a symbol could expand to as many bytes as there are
  93.  * LZW symbols, so we allocate LZW_TABLE_SIZE bytes for the stack.
  94.  * (This is conservative since that number includes the raw-byte symbols.)
  95.  *
  96.  * The tables are allocated from FAR heap space since they would use up
  97.  * rather a lot of the near data space in a PC.
  98.  */
  99.  
  100. static UINT16 FAR *symbol_head; /* => table of prefix symbols */
  101. static UINT8  FAR *symbol_tail; /* => table of suffix bytes */
  102. static UINT8  FAR *symbol_stack; /* stack for symbol expansions */
  103. static UINT8  FAR *sp;        /* stack pointer */
  104.  
  105. /* Static state for interlaced image processing */
  106.  
  107. static boolean is_interlaced;    /* TRUE if have interlaced image */
  108. static big_sarray_ptr interlaced_image;    /* full image in interlaced order */
  109. static long cur_row_number;    /* need to know actual row number */
  110. static long pass2_offset;    /* # of pixel rows in pass 1 */
  111. static long pass3_offset;    /* # of pixel rows in passes 1&2 */
  112. static long pass4_offset;    /* # of pixel rows in passes 1,2,3 */
  113.  
  114.  
  115. /* Forward declarations */
  116. METHODDEF void load_interlaced_image PP((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
  117. METHODDEF void get_interlaced_row PP((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
  118.  
  119.  
  120.  
  121. LOCAL int
  122. ReadByte (compress_info_ptr cinfo)
  123. /* Read next byte from GIF file */
  124. {
  125.   register FILE * infile = cinfo->input_file;
  126.   int c;
  127.  
  128.   if ((c = getc(infile)) == EOF)
  129.     ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  130.   return c;
  131. }
  132.  
  133.  
  134. LOCAL int
  135. GetDataBlock (compress_info_ptr cinfo, char *buf)
  136. /* Read a GIF data block, which has a leading count byte */
  137. /* A zero-length block marks the end of a data block sequence */
  138. {
  139.   int count;
  140.  
  141.   count = ReadByte(cinfo);
  142.   if (count > 0) {
  143.     if (! ReadOK(cinfo->input_file, buf, count))
  144.       ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  145.   }
  146.   return count;
  147. }
  148.  
  149.  
  150. LOCAL void
  151. SkipDataBlocks (compress_info_ptr cinfo)
  152. /* Skip a series of data blocks, until a block terminator is found */
  153. {
  154.   char buf[256];
  155.  
  156.   while (GetDataBlock(cinfo, buf) > 0)
  157.     /* skip */;
  158. }
  159.  
  160.  
  161. LOCAL void
  162. ReInitLZW (void)
  163. /* (Re)initialize LZW state; shared code for startup and Clear processing */
  164. {
  165.   code_size = input_code_size+1;
  166.   limit_code = clear_code << 1;    /* 2^code_size */
  167.   max_code = clear_code + 2;    /* first unused code value */
  168.   sp = symbol_stack;        /* init stack to empty */
  169. }
  170.  
  171.  
  172. LOCAL void
  173. InitLZWCode (void)
  174. /* Initialize for a series of LZWReadByte (and hence GetCode) calls */
  175. {
  176.   /* GetCode initialization */
  177.   last_byte = 2;        /* make safe to "recopy last two bytes" */
  178.   last_bit = 0;            /* nothing in the buffer */
  179.   cur_bit = 0;            /* force buffer load on first call */
  180.   out_of_blocks = FALSE;
  181.  
  182.   /* LZWReadByte initialization */
  183.   clear_code = 1 << input_code_size; /* compute special code values */
  184.   end_code = clear_code + 1;    /* note that these do not change */
  185.   first_time = TRUE;
  186.   ReInitLZW();
  187. }
  188.  
  189.  
  190. LOCAL int
  191. GetCode (compress_info_ptr cinfo)
  192. /* Fetch the next code_size bits from the GIF data */
  193. /* We assume code_size is less than 16 */
  194. {
  195.   register INT32 accum;
  196.   int offs, ret, count;
  197.  
  198.   if ( (cur_bit+code_size) > last_bit) {
  199.     /* Time to reload the buffer */
  200.     if (out_of_blocks) {
  201.       TRACEMS(cinfo->emethods, 1, "Ran out of GIF bits");
  202.       return end_code;        /* fake something useful */
  203.     }
  204.     /* preserve last two bytes of what we have -- assume code_size <= 16 */
  205.     code_buf[0] = code_buf[last_byte-2];
  206.     code_buf[1] = code_buf[last_byte-1];
  207.     /* Load more bytes; set flag if we reach the terminator block */
  208.     if ((count = GetDataBlock(cinfo, &code_buf[2])) == 0) {
  209.       out_of_blocks = TRUE;
  210.       TRACEMS(cinfo->emethods, 1, "Ran out of GIF bits");
  211.       return end_code;        /* fake something useful */
  212.     }
  213.     /* Reset counters */
  214.     cur_bit = (cur_bit - last_bit) + 16;
  215.     last_byte = 2 + count;
  216.     last_bit = last_byte * 8;
  217.   }
  218.  
  219.   /* Form up next 24 bits in accum */
  220.   offs = cur_bit >> 3;        /* byte containing cur_bit */
  221. #ifdef CHAR_IS_UNSIGNED
  222.   accum = code_buf[offs+2];
  223.   accum <<= 8;
  224.   accum |= code_buf[offs+1];
  225.   accum <<= 8;
  226.   accum |= code_buf[offs];
  227. #else
  228.   accum = code_buf[offs+2] & 0xFF;
  229.   accum <<= 8;
  230.   accum |= code_buf[offs+1] & 0xFF;
  231.   accum <<= 8;
  232.   accum |= code_buf[offs] & 0xFF;
  233. #endif
  234.  
  235.   /* Right-align cur_bit in accum, then mask off desired number of bits */
  236.   accum >>= (cur_bit & 7);
  237.   ret = ((int) accum) & ((1 << code_size) - 1);
  238.   
  239.   cur_bit += code_size;
  240.   return ret;
  241. }
  242.  
  243.  
  244. LOCAL int
  245. LZWReadByte (compress_info_ptr cinfo)
  246. /* Read an LZW-compressed byte */
  247. {
  248.   static int oldcode;        /* previous LZW symbol */
  249.   static int firstcode;        /* first byte of oldcode's expansion */
  250.   register int code;        /* current working code */
  251.   int incode;            /* saves actual input code */
  252.  
  253.   /* First time, just eat the expected Clear code(s) and return next code, */
  254.   /* which is assumed to be a raw byte. */
  255.   if (first_time) {
  256.     first_time = FALSE;
  257.     do {
  258.       code = GetCode(cinfo);
  259.     } while (code == clear_code);
  260.     firstcode = oldcode = code;    /* make firstcode, oldcode valid! */
  261.     return code;
  262.   }
  263.  
  264.   /* If any codes are stacked from a previously read symbol, return them */
  265.   if (sp > symbol_stack)
  266.     return (int) *(--sp);
  267.  
  268.   code = GetCode(cinfo);
  269.  
  270.   if (code == clear_code) {
  271.     /* Reinit static state, swallow any extra Clear codes, and return */
  272.     ReInitLZW();
  273.     do {
  274.       code = GetCode(cinfo);
  275.     } while (code == clear_code);
  276.     firstcode = oldcode = code; /* gotta reinit these too */
  277.     return code;
  278.   }
  279.  
  280.   if (code == end_code) {
  281.     /* Skip the rest of the image, unless GetCode already read terminator */
  282.     if (! out_of_blocks)
  283.       SkipDataBlocks(cinfo);
  284.     return -1;
  285.   }
  286.  
  287.   /* Normal raw byte or LZW symbol */
  288.   incode = code;        /* save for a moment */
  289.   
  290.   if (code >= max_code) {    /* special case for not-yet-defined symbol */
  291.     *sp++ = (UINT8) firstcode;    /* it will be defined as oldcode/firstcode */
  292.     code = oldcode;
  293.   }
  294.  
  295.   /* If it's a symbol, expand it into the stack */
  296.   while (code >= clear_code) {
  297.     *sp++ = symbol_tail[code];    /* tail of symbol: a simple byte value */
  298.     code = symbol_head[code];    /* head of symbol: another LZW symbol */
  299.   }
  300.   /* At this point code just represents a raw byte */
  301.   firstcode = code;        /* save for possible future use */
  302.  
  303.   /* If there's room in table, */
  304.   if ((code = max_code) < LZW_TABLE_SIZE) {
  305.     /* Define a new symbol = prev sym + head of this sym's expansion */
  306.     symbol_head[code] = oldcode;
  307.     symbol_tail[code] = (UINT8) firstcode;
  308.     max_code++;
  309.     /* Is it time to increase code_size? */
  310.     if ((max_code >= limit_code) && (code_size < MAX_LZW_BITS)) {
  311.       code_size++;
  312.       limit_code <<= 1;        /* keep equal to 2^code_size */
  313.     }
  314.   }
  315.   
  316.   oldcode = incode;        /* save last input symbol for future use */
  317.   return firstcode;        /* return first byte of symbol's expansion */
  318. }
  319.  
  320.  
  321. LOCAL void
  322. ReadColorMap (compress_info_ptr cinfo, int cmaplen, JSAMPARRAY cmap)
  323. /* Read a GIF colormap */
  324. {
  325.   int i;
  326.  
  327.   for (i = 0; i < cmaplen; i++) {
  328. /*
  329.     GrSetColor(i,ReadByte(cinfo),ReadByte(cinfo),ReadByte(cinfo));
  330. */
  331.     cmap[CM_RED][i]   = (JSAMPLE) ReadByte(cinfo);
  332.     cmap[CM_GREEN][i] = (JSAMPLE) ReadByte(cinfo);
  333.     cmap[CM_BLUE][i]  = (JSAMPLE) ReadByte(cinfo);
  334.  
  335.     
  336.   }
  337. }
  338.  
  339.  
  340. LOCAL void
  341. DoExtension (compress_info_ptr cinfo)
  342. /* Process an extension block */
  343. /* Currently we ignore 'em all */
  344. {
  345.   int extlabel;
  346.  
  347.   /* Read extension label byte */
  348.   extlabel = ReadByte(cinfo);
  349.   TRACEMS1(cinfo->emethods, 1, "Ignoring GIF extension block of type 0x%02x",
  350.        extlabel);
  351.   /* Skip the data block(s) associated with the extension */
  352.   SkipDataBlocks(cinfo);
  353. }
  354.  
  355.  
  356. /*
  357.  * Read the file header; return image size and component count.
  358.  */
  359.  
  360. METHODDEF void
  361. input_init (compress_info_ptr cinfo)
  362. {
  363.   char hdrbuf[10];        /* workspace for reading control blocks */
  364.   UINT16 width, height;        /* image dimensions */
  365.   int colormaplen, aspectRatio;
  366.   int c;
  367.  
  368.   /* Allocate space to store the colormap */
  369.   gifcolormap = (*cinfo->emethods->alloc_small_sarray)
  370.         ((long) MAXCOLORMAPSIZE, (long) NUMCOLORS);
  371.  
  372.   /* Read and verify GIF Header */
  373.   if (! ReadOK(cinfo->input_file, hdrbuf, 6))
  374.     ERREXIT(cinfo->emethods, "Not a GIF file");
  375.   if (strncmp(hdrbuf, "GIF", 3) != 0)
  376.     ERREXIT(cinfo->emethods, "Not a GIF file");
  377.   /* Check for expected version numbers.
  378.    * If unknown version, give warning and try to process anyway;
  379.    * this is per recommendation in GIF89a standard.
  380.    */
  381.   if ((strncmp(hdrbuf+3, "87a", 3) != 0) &&
  382.       (strncmp(hdrbuf+3, "89a", 3) != 0))
  383.     TRACEMS3(cinfo->emethods, 1,
  384.          "Warning: unexpected GIF version number '%c%c%c'",
  385.          hdrbuf[3], hdrbuf[4], hdrbuf[5]);
  386.  
  387.   /* Read and decipher Logical Screen Descriptor */
  388.   if (! ReadOK(cinfo->input_file, hdrbuf, 7))
  389.     ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  390.   width = LM_to_uint(hdrbuf[0],hdrbuf[1]);
  391.   height = LM_to_uint(hdrbuf[2],hdrbuf[3]);
  392.   colormaplen = 2 << (hdrbuf[4] & 0x07);
  393.   /* we ignore the color resolution, sort flag, and background color index */
  394.   aspectRatio = hdrbuf[6] & 0xFF;
  395.   if (aspectRatio != 0 && aspectRatio != 49)
  396.     TRACEMS(cinfo->emethods, 1, "Warning: nonsquare pixels in input");
  397.  
  398.   /* Read global colormap if header indicates it is present */
  399.   if (BitSet(hdrbuf[4], COLORMAPFLAG))
  400.     ReadColorMap(cinfo, colormaplen, gifcolormap);
  401.  
  402.   /* Scan until we reach start of desired image.
  403.    * We don't currently support skipping images, but could add it easily.
  404.    */
  405.   for (;;) {
  406.     c = ReadByte(cinfo);
  407.  
  408.     if (c == ';')        /* GIF terminator?? */
  409.       ERREXIT(cinfo->emethods, "Too few images in GIF file");
  410.  
  411.     if (c == '!') {        /* Extension */
  412.       DoExtension(cinfo);
  413.       continue;
  414.     }
  415.     
  416.     if (c != ',') {        /* Not an image separator? */
  417.       TRACEMS1(cinfo->emethods, 1, "Bogus input char 0x%02x, ignoring", c);
  418.       continue;
  419.     }
  420.  
  421.     /* Read and decipher Local Image Descriptor */
  422.     if (! ReadOK(cinfo->input_file, hdrbuf, 9))
  423.       ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  424.     /* we ignore top/left position info, also sort flag */
  425.     width = LM_to_uint(hdrbuf[4],hdrbuf[5]);
  426.     height = LM_to_uint(hdrbuf[6],hdrbuf[7]);
  427.     is_interlaced = BitSet(hdrbuf[8], INTERLACE);
  428.     colormaplen = 2 << (hdrbuf[8] & 0x07);
  429.  
  430.     /* Read local colormap if header indicates it is present */
  431.     /* Note: if we wanted to support skipping images, */
  432.     /* we'd need to skip rather than read colormap for ignored images */
  433.     if (BitSet(hdrbuf[8], COLORMAPFLAG))
  434.       ReadColorMap(cinfo, colormaplen, gifcolormap);
  435.  
  436.     input_code_size = ReadByte(cinfo); /* get minimum-code-size byte */
  437.     if (input_code_size < 2 || input_code_size >= MAX_LZW_BITS)
  438.       ERREXIT1(cinfo->emethods, "Bogus codesize %d", input_code_size);
  439.  
  440.     /* Reached desired image, so break out of loop */
  441.     /* If we wanted to skip this image, */
  442.     /* we'd call SkipDataBlocks and then continue the loop */
  443.     break;
  444.   }
  445.  
  446.   /* Prepare to read selected image: first initialize LZW decompressor */
  447.   symbol_head = (UINT16 FAR *) (*cinfo->emethods->alloc_medium)
  448.                 (LZW_TABLE_SIZE * SIZEOF(UINT16));
  449.   symbol_tail = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
  450.                 (LZW_TABLE_SIZE * SIZEOF(UINT8));
  451.   symbol_stack = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
  452.                 (LZW_TABLE_SIZE * SIZEOF(UINT8));
  453.   InitLZWCode();
  454.  
  455.   /*
  456.    * If image is interlaced, we read it into a full-size sample array,
  457.    * decompressing as we go; then get_input_row selects rows from the
  458.    * sample array in the proper order.
  459.    */
  460.   if (is_interlaced) {
  461.     /* We request the big array now, but can't access it until the pipeline
  462.      * controller causes all the big arrays to be allocated.  Hence, the
  463.      * actual work of reading the image is postponed until the first call
  464.      * of get_input_row.
  465.      */
  466.     interlaced_image = (*cinfo->emethods->request_big_sarray)
  467.         ((long) width, (long) height, 1L);
  468.     cinfo->methods->get_input_row = load_interlaced_image;
  469.     cinfo->total_passes++;    /* count file reading as separate pass */
  470.   }
  471.  
  472.   /* Return info about the image. */
  473.   cinfo->input_components = NUMCOLORS;
  474.   cinfo->in_color_space = CS_RGB;
  475.   cinfo->image_width = width;
  476.   cinfo->image_height = height;
  477.   cinfo->data_precision = 8;    /* always, even if 12-bit JSAMPLEs */
  478. }
  479.  
  480.  
  481. /*
  482.  * Read one row of pixels.
  483.  * This version is used for noninterlaced GIF images:
  484.  * we read directly from the GIF file.
  485.  */
  486.  
  487. METHODDEF void
  488. get_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  489. {
  490.   register JSAMPROW ptr0, ptr1, ptr2;
  491.   register long col;
  492.   register int c;
  493.   
  494.   ptr0 = (JSAMPROW) **((JSAMPIMAGE)pixel_row);
  495. /*
  496.   ptr1 = pixel_row[1];
  497.   ptr2 = pixel_row[2];
  498. */
  499.   for (col = cinfo->image_width; col > 0; col--) {
  500.     if ((c = LZWReadByte(cinfo)) < 0)
  501.       ERREXIT(cinfo->emethods, "Premature end of GIF image");
  502.     *ptr0++ = (JSAMPLE) c;
  503. /*
  504.     *ptr1++ = colormap[CM_GREEN][c];
  505.     *ptr2++ = colormap[CM_BLUE][c];
  506. */
  507.   }
  508. }
  509.  
  510.  
  511. /*
  512.  * Read one row of pixels.
  513.  * This version is used for the first call on get_input_row when
  514.  * reading an interlaced GIF file: we read the whole image into memory.
  515.  */
  516.  
  517. METHODDEF void
  518. load_interlaced_image (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  519. {
  520.   JSAMPARRAY image_ptr;
  521.   register JSAMPROW sptr;
  522.   register long col;
  523.   register int c;
  524.   long row;
  525.  
  526.   /* Read the interlaced image into the big array we've created. */
  527.   for (row = 0; row < cinfo->image_height; row++) {
  528.     (*cinfo->methods->progress_monitor) (cinfo, row, cinfo->image_height);
  529.     image_ptr = (*cinfo->emethods->access_big_sarray)
  530.             (interlaced_image, row, TRUE);
  531.     sptr = image_ptr[0];
  532.     for (col = cinfo->image_width; col > 0; col--) {
  533.       if ((c = LZWReadByte(cinfo)) < 0)
  534.     ERREXIT(cinfo->emethods, "Premature end of GIF image");
  535.       *sptr++ = (JSAMPLE) c;
  536.     }
  537.   }
  538.   cinfo->completed_passes++;
  539.  
  540.   /* Replace method pointer so subsequent calls don't come here. */
  541.   cinfo->methods->get_input_row = get_interlaced_row;
  542.   /* Initialize for get_interlaced_row, and perform first call on it. */
  543.   cur_row_number = 0;
  544.   pass2_offset = (cinfo->image_height + 7L) / 8L;
  545.   pass3_offset = pass2_offset + (cinfo->image_height + 3L) / 8L;
  546.   pass4_offset = pass3_offset + (cinfo->image_height + 1L) / 4L;
  547.  
  548.   get_interlaced_row(cinfo, pixel_row);
  549. }
  550.  
  551.  
  552. /*
  553.  * Read one row of pixels.
  554.  * This version is used for interlaced GIF images:
  555.  * we read from the big in-memory image.
  556.  */
  557.  
  558. METHODDEF void
  559. get_interlaced_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  560. {
  561.   JSAMPARRAY image_ptr;
  562.   register JSAMPROW sptr, ptr0, ptr1, ptr2;
  563.   register long col;
  564.   register int c;
  565.   long irow;
  566.  
  567.   /* Figure out which row of interlaced image is needed, and access it. */
  568.   switch ((int) (cur_row_number & 7L)) {
  569.   case 0:            /* first-pass row */
  570.     irow = cur_row_number >> 3;
  571.     break;
  572.   case 4:            /* second-pass row */
  573.     irow = (cur_row_number >> 3) + pass2_offset;
  574.     break;
  575.   case 2:            /* third-pass row */
  576.   case 6:
  577.     irow = (cur_row_number >> 2) + pass3_offset;
  578.     break;
  579.   default:            /* fourth-pass row */
  580.     irow = (cur_row_number >> 1) + pass4_offset;
  581.     break;
  582.   }
  583.   image_ptr = (*cinfo->emethods->access_big_sarray)
  584.             (interlaced_image, irow, FALSE);
  585.   /* Scan the row, expand colormap, and output */
  586.   sptr = image_ptr[0];
  587.   ptr0 = (JSAMPROW) **((JSAMPIMAGE)pixel_row);
  588. /*
  589.   ptr1 = pixel_row[1];
  590.   ptr2 = pixel_row[2];
  591. */
  592.   for (col = cinfo->image_width; col > 0; col--) {
  593. /*
  594.     c = GETJSAMPLE(*sptr++);
  595. */
  596.     *ptr0++ = GETJSAMPLE(*sptr++);
  597. /*
  598.     *ptr1++ = colormap[CM_GREEN][c];
  599.     *ptr2++ = colormap[CM_BLUE][c];
  600. */
  601.   }
  602.   cur_row_number++;        /* for next time */
  603. }
  604.  
  605.  
  606. /*
  607.  * Finish up at the end of the file.
  608.  */
  609.  
  610. METHODDEF void
  611. input_term (compress_info_ptr cinfo)
  612. {
  613.   /* no work (we let free_all release the workspace) */
  614. }
  615.  
  616.  
  617. /*
  618.  * The method selection routine for GIF format input.
  619.  * Note that this must be called by the user interface before calling
  620.  * jpeg_compress.  If multiple input formats are supported, the
  621.  * user interface is responsible for discovering the file format and
  622.  * calling the appropriate method selection routine.
  623.  */
  624.  
  625. GLOBAL void
  626. jselrgif (compress_info_ptr cinfo)
  627. {
  628.   cinfo->methods->input_init = input_init;
  629.   cinfo->methods->get_input_row = get_input_row; /* assume uninterlaced */
  630.   cinfo->methods->input_term = input_term;
  631. }
  632.  
  633. #endif /* GIF_SUPPORTED */
  634.